package cn.wolfcode.wechat.util;

import com.alibaba.fastjson.JSONObject;

import java.util.Date;

public class TokenUtil {
    public static final String TOKEN = "wolfCode";
    public static final String APPID = "wx29a5e36bb88b1be6";
    public static final String APPSECRET = "513dc7c9d623aeca655819c96f788075";

    //创建菜单
    public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    //获取accessToken
    public static final String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    //删除菜单
    public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
    //模板消息
    public static final String MODEL_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

    public static String accessToken;   //调用各个接口的凭据
    public static Long expires;   //失效时间
    /**
     * 创建菜单
     * @param json
     * @return
     * @throws Exception
     */
    public static void createMenu(String json) throws Exception {
        String url = CREATE_MENU_URL.replace("ACCESS_TOKEN",accessToken());
        String result = HttpUtil.doPostJson(url,json);
        System.out.println(result);
    }

    /**
     * 获取accessToken凭证
     * @return
     * @throws Exception
     */
    public static String accessToken(){
        if (accessToken==null||new Date().getTime()>expires){
            String url = ACCESS_TOKEN.replace("APPID", APPID)
                    .replace("APPSECRET", APPSECRET);
            String result = HttpUtil.doGet(url);
            JSONObject json = JSONObject.parseObject(result);
            //凭据
            accessToken = json.getString("access_token");
            //有效期
            Long expires_in = json.getLong("expires_in");
            expires = new Date().getTime() + (expires_in - 60) * 1000;
        }
        return accessToken;
    }

    /**
     * 删除菜单
     * @return
     */
    public static void deleteMenu(){
        String url = DELETE_MENU_URL.replace("ACCESS_TOKEN",accessToken());
        String str = HttpUtil.doGet(url);
        System.out.println(str);
    }

    /**
     * 模板消息
     */
    public static void modelMessage(){
        String url = MODEL_MESSAGE.replace("ACCESS_TOKEN",accessToken());
        String str = HttpUtil.doPostJson(url, sendJson());
        System.out.println(str);
    }

    private static String sendJson() {
        String str = "{\n" +
                "           \"touser\":\"olXwG6FwSzFM6lPKQW7-xjgltT6o\",\n" +
                "           \"template_id\":\"T-FVcLCT8r0FsYxTnPF0t6mLg-XC74bMWDUc47Fopjg\",\n" +
                "           \"url\":\"https://www.wolfcode.cn/\",        \n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"恭喜你购买成功！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"product\":{\n" +
                "                       \"value\":\"巧克力\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"amount\": {\n" +
                "                       \"value\":\"39.8元\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"time\": {\n" +
                "                       \"value\":\"2021年9月22日\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"欢迎再次购买！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   }\n" +
                "           }\n" +
                "       }";
        return str;
    }


    public static void main(String[] args) throws Exception {
        /*String json = " {\n" +
                "     \"button\":[\n" +
                "     {\t\n" +
                "          \"type\":\"click\",\n" +
                "          \"name\":\"今日歌曲\",\n" +
                "          \"key\":\"V1001_TODAY_MUSIC\"\n" +
                "      },\n" +
                "      {\n" +
                "           \"name\":\"菜单\",\n" +
                "           \"sub_button\":[\n" +
                "           {\t\n" +
                "               \"type\":\"view\",\n" +
                "               \"name\":\"搜索\",\n" +
                "               \"url\":\"http://www.soso.com/\"\n" +
                "            }, {\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"赞一下我们\",\n" +
                "               \"key\":\"V1001_GOOD\"\n" +
                "            }]\n" +
                "       }]\n" +
                " }";
       createMenu(json);*/
       //accessToken();
       //deleteMenu();
        modelMessage();
    }
}
